#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>

int main(int argc, char const *argv[])
{
    printf("JSON version: %s\n", cJSON_Version());
    char json[] = "{\"name\":\"Tom\",\"age\":18,\"gender\":\"male\"}";  
    
    // 1.创建JSON对象
    cJSON *obj = cJSON_Parse(json);
    if (obj == NULL)
    {
        printf("parse json failed\n");
        return -1;
    }

    // 2.获取JSON中key对应的value
    cJSON *name = cJSON_GetObjectItem(obj, "name");
    cJSON *age = cJSON_GetObjectItem(obj, "age");
    cJSON *gender = cJSON_GetObjectItem(obj, "gender");

    // 3.根据value的类型，获取对应的值
    char *name_str = cJSON_GetStringValue(name);
    int age_int = cJSON_GetNumberValue(age);
    char *gender_str = cJSON_GetStringValue(gender);

    printf("name: %s\n", name_str);
    printf("age: %d\n", age_int);
    printf("gender: %s\n", gender_str);

    return 0;
}
